package ch.janotta.slawo.da.tables;

import java.util.ArrayList;
import java.util.List;

import ch.janotta.slawo.da.Focus;
import ch.janotta.slawo.da.modifier.Modifier;
import ch.janotta.slawo.utilities.Dice;

public abstract class BackgroundStartingModifiersTable {
  private List<Modifier> usedModifiers;

  public Modifier getNonDuplicateRandomModifier(boolean firstUse) {
    if (firstUse) {
      usedModifiers = new ArrayList<>();
    }

    Modifier value = getRandomModifier();
    if (firstUse) {
      usedModifiers.add(value);
    } else if (usedModifiers.size() < getTableSize()) {
      while (usedModifiers.contains(value)) {
        value = getRandomModifier();
      }
    }
    return value;
  }

  public Modifier getRandomModifier() {
    int die1Result = Dice.getD6();
    int die2Result = Dice.getD6();
    int result = die1Result + die2Result;
    int tableIndex = result - 2; // 2d6 starts with 2, array indexes start with 0
    return getTableResult(tableIndex);
  }

  public Modifier getModifier(int diceRoll) {
    if (diceRoll >= 2 && diceRoll <= 12) {
      return getTableResult(diceRoll - 2);
    } else {
      return null;
    }
  }

  public abstract Modifier getTableResult(int index);

  public int getTableSize() {
    return 8;
  }

  public abstract Modifier[] getAllStartingModifiers();

  protected Focus pickOneRandomFocus(Focus focus1, Focus focus2) {
    Focus randomFocus = null;
    int randomNumber = Dice.getRandomNumber(2);
    if (randomNumber == 1) {
      randomFocus = focus1;
    } else if (randomNumber == 2) {
      randomFocus = focus2;
    }
    return randomFocus;
  }
}
